package org.tech4j.common.core.request.param;

import com.google.common.collect.Maps;
import jakarta.servlet.http.HttpServletRequest;
import org.tech4j.common.spi.Join;

import java.util.Enumeration;
import java.util.Map;
import java.util.Objects;

/**
 * @program: tech4j-common
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/2 20:08
 **/
@Join
public class HeaderParameterData implements ParameterData {

    @Override
    public String builder(String paramName, HttpServletRequest request) {
        return request.getHeader(paramName);
    }

    @Override
    public Map<String, Object> builderMap(String paramName, HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        if (Objects.isNull(headerNames)) {
            return null;
        }
        Map<String, Object> map = Maps.newHashMap();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            map.put(headerName, headerValue);
        }
        return map;
    }
}
